package cn.hdu.liang.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EvenChecker implements Runnable {
    private IntGenerator gen;
    private final int id;

    public EvenChecker(IntGenerator gen, int id) {
        this.gen = gen;
        this.id = id;
    }

    @Override
    public void run() {
        while(!gen.isCanceled()) {
            int val = gen.next();
            if(val % 2 != 0) {
                System.out.println(val + "不是偶数");
                gen.cacel();
            }
        }
    }

    public static void test(IntGenerator gp, int count) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i = 0; i < count; i++) {
            exec.execute(new EvenChecker(gp, i));
        }
        exec.shutdown();
    }

    public static void test(IntGenerator gp) {
        test(gp, 10);
    }
}
